package com.code.leetcode.offer;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/3 22:37
 */
public class The53 {
    public int search(int[] nums, int target) {
        int len = nums.length;
        if (len == 0) return 0;

        int left = 0, right = len -1;
        int mid = left + (right - left) / 2;

        while (left <= right) {
            mid = left + (right - left) / 2;
            if (nums[mid] == target) break;
            else if (nums[mid] > target) right = mid - 1;
            else left = mid + 1;
        }

        int num = 0;

        if (nums[mid] == target) {
            left = mid;
            right = mid + 1;
            while (left > 0 && nums[left] == target) {
                num++;
                left--;
            }
            while (right < len && nums[right] == target) {
                right++;
                num++;
            }
        }
        return num;
    }
}
